package com.lh.api.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.lh.api.dao.ColumnMapper;
import com.lh.api.dao.ShopSortMapper;
import com.lh.api.vo.ColumnVo;
import com.lh.api.vo.SearchVo;
import com.lh.common.entity.admin.Column;
import com.lh.api.service.ColumnService;
import com.lh.common.enums.ColumnTypeEnum;
import com.lh.common.enums.ShopSortTypeEnum;
import com.lh.common.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Xyw
 * @ClassName:
 * @Description: (类作用)
 * @date: 2020-06-29 10:58
 */
@Service
public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, Column> implements ColumnService {

    @Autowired
    private ShopSortMapper shopSortMapper;

    @Override
    public List<ColumnVo> columnList() {
        return this.baseMapper.columnList();
    }

    public Column getColumnStatus(Long cid){
        return this.baseMapper.getColumnStatus(cid);
    }

    private void addData(List<SearchVo> list) {
        list.forEach(searchVo -> {
            searchVo.setPercent(new BigDecimal(0));
            if(searchVo.getPortionNum() > 0 ){
                searchVo.setPercent(new BigDecimal(searchVo.getCurrenPortion() * 100).divide(new BigDecimal(searchVo.getPortionNum()), 2, BigDecimal.ROUND_HALF_UP));
            }
        });
    }

    @Override
    public PageUtils<SearchVo> getColumnShopList(Long cid, ShopSortTypeEnum sortType, ColumnTypeEnum columnType, String qureyTime, Integer pageNum, Integer pageSize) {
        if(pageNum != null && pageSize != null){
            PageHelper.startPage(pageNum, pageSize);
        }
        String sortName = "create_time";
        String sortOrder = "DESC";
        if (ShopSortTypeEnum.PRICEDESC.equals(sortType)){
            sortName = "price";
            sortOrder = "DESC";
        }else if (ShopSortTypeEnum.PRICEASC.equals(sortType)){
            sortName = "price";
            sortOrder = "ASC";
        }

        List<SearchVo> searchVos = null;
        if(ColumnTypeEnum.ACTIVITY.equals(columnType)){
            searchVos = this.baseMapper.activityShopList(cid, qureyTime, sortName, sortOrder);
        }else {
            searchVos = this.baseMapper.categoryShopList(cid, qureyTime, sortName, sortOrder);
        }

        addData(searchVos);
        return new PageUtils<>(searchVos);
    }

    @Override
    public Column getColumnName(String name) {
        return this.baseMapper.queryColumnList(name);
    }

    @Override
    @Transactional
    public int addColumn(Column column) {
        return 0;
    }

    @Override
    public int updateColumn(Column column) {
        return this.baseMapper.updateColumn(column);
    }
}
